package com.acme.bankapp.domain;

import com.acme.bankapp.domain.accounts.Account;
import com.acme.bankapp.domain.accounts.AccountType;
import com.acme.bankapp.domain.accounts.CheckingAccount;
import com.acme.bankapp.domain.accounts.SavingsAccount;

/**
 * 
 * Holds information about the client's Name, Gender and Account.
 * Provides methods to get client info.
 * 
 */
public class Client {

	private Account account;
	private String name;
	private Gender gender;

	/**
	 * Constructs client by name and gender with a Savings account.
	 * @param name full name.
	 * @param gender desired gender.
	 */
	public Client(String name, Gender gender) {
		account = new SavingsAccount(this); 
		
		this.name = name;
		this.gender = gender;
	
	}
	
	/**
	 * Constructs client by name and gender with a specified type account.
	 * @param name full name.
	 * @param gender desired gender.
	 * @param accountType specified account type. 
	 * @param overdraft Overdraft value to be set (optional).
	 */
	public Client(String name, Gender gender, AccountType accountType, double... overdraft) {
		this(name, gender);
		
		switch (accountType) {
		case SAVINGS:
			account = new SavingsAccount(this);
			break;
		case CHECKING:
			if (overdraft.length > 0) {
				account = new CheckingAccount(this, overdraft[0]);				
			} else {
				account = new CheckingAccount(this, 0.);
			}
			break;
		default:
			account = new SavingsAccount(this);
		}
	}
	
	
	/** 
	 * Provides single-line description of form:
	 * Name, Gender: Balance.
	 */
	@Override
	public String toString() {
		return (new StringBuffer()).append(name).append(", ").append(gender).append(": ").append(account.getBalance()).toString();
	}
	
	/**
	 * @return String salutation (ex.: Mr. Bill Gates).
	 */
	public String getSalutation() {
		return "" + gender + " " + getName();
	}
	
	/**
	 * @return Client's name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Sets the name equal to the specified String.
	 * @param name Name to be set.
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * @return clients Gender.
	 */
	public Gender getGender() {
		return gender;
	}
	
	/**
	 * Sets specified gender. 
	 * @param gender Gender to be set.
	 */
	public void setGender(Gender gender) {
		this.gender = gender;
	}
	
	/**
	 * @return Account object. 
	 */
	public Account getAccount() {
		return account;
	}

	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((account == null) ? 0 : account.hashCode());
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Client other = (Client) obj;
		if (account == null) {
			if (other.account != null)
				return false;
		} else if (!account.equals(other.account)) {
			return false;
		}
		if (gender != other.gender) {
			return false;
		}
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

	
}
